package com.linxiao.framework.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.LayoutManager;
import android.support.v7.widget.RecyclerView.State;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.TypedValue;
import android.view.View;

/**
 *  RecyclerView 的 GridLayoutManager 形式下的分割线
 * * Created by Extends on 2016/8/10 0010.
 */
public class DividerGridItemDecoration extends RecyclerView.ItemDecoration
{
    private BaseRecyclerAdapter adapter;
    private Context context;
    private static final int[] ATTRS = new int[] { android.R.attr.listDivider };
    private Paint mPaint;
    private Drawable mDivider;
    private int mDividerHeight = 1;//分割线高度，默认为1px

    public DividerGridItemDecoration(Context context)
    {
        this.context = context;
        final TypedArray a = context.obtainStyledAttributes(ATTRS);
        mDivider = a.getDrawable(0);
        a.recycle();
        mDividerHeight = mDivider.getIntrinsicHeight();
    }

    /**
     * @param context
     * @param drawableId  分割线图片
     */
    public DividerGridItemDecoration(Context context, int drawableId) {
        this.context = context;
        mDivider = ContextCompat.getDrawable(context, drawableId);
        mDividerHeight = mDivider.getIntrinsicHeight();
    }

    /**
     *
     * @param context
     * @param dividerHeight 分割线高度，单位sp
     * @param dividerColor 分割线颜色
     */
    public DividerGridItemDecoration(Context context,float dividerHeight, int dividerColor) {
        this(context,dividerHeight,TypedValue.COMPLEX_UNIT_SP,dividerColor);
    }

    /**
     *
     * @param context
     * @param dividerHeight 分割线高度
     * @param typedValue 分割线高度的单位
     * @param dividerColor 分割线颜色
     */
    public DividerGridItemDecoration(Context context,float dividerHeight,int typedValue, int dividerColor) {
        this.context = context;
        mDividerHeight = (int)applyDimension(typedValue,dividerHeight);
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(dividerColor);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, State state)
    {
        drawHorizontal(c, parent);
        drawVertical(c, parent);

    }

    private int getSpanCount(RecyclerView parent)
    {
        // 列数
        int spanCount = -1;
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager)
        {
            spanCount = ((GridLayoutManager) layoutManager).getSpanCount();
        } else if (layoutManager instanceof StaggeredGridLayoutManager)
        {
            spanCount = ((StaggeredGridLayoutManager) layoutManager)
                    .getSpanCount();
        }else if(layoutManager instanceof LinearLayoutManager){
            spanCount = 1;
        }
        return spanCount;
    }

    public void drawHorizontal(Canvas c, RecyclerView parent)
    {
        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++)
        {
            final View child = parent.getChildAt(i);
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int left = child.getLeft() - params.leftMargin;
            final int right = child.getRight() + params.rightMargin+mDividerHeight;
//                    + mDivider.getIntrinsicWidth();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDividerHeight;
            if (mDivider != null) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
            if (mPaint != null) {
                c.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

    public void drawVertical(Canvas c, RecyclerView parent)
    {
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++)
        {
            final View child = parent.getChildAt(i);

            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
                    .getLayoutParams();
            final int top = child.getTop() - params.topMargin;
            final int bottom = child.getBottom() + params.bottomMargin;
            final int left = child.getRight() + params.rightMargin;
            final int right = left+mDividerHeight;// + mDivider.getIntrinsicWidth();

            if (mDivider != null) {
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
            if (mPaint != null) {
                c.drawRect(left, top, right, bottom, mPaint);
            }
        }
    }

    /**
     * 判断是否是最后一列
     * @param parent
     * @param pos
     * @param spanCount
     * @param childCount
     * @return
     */
    private boolean isLastColum(RecyclerView parent, int pos, int spanCount,
                                int childCount) {
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager)
        {
            if ((pos + 1) % spanCount == 0)// 如果是最后一列，则不需要绘制右边
            {
                return true;
            }
        } else if (layoutManager instanceof StaggeredGridLayoutManager)
        {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            if (orientation == StaggeredGridLayoutManager.VERTICAL)
            {
                if ((pos + 1) % spanCount == 0)// 如果是最后一列，则不需要绘制右边
                {
                    return true;
                }
            } else
            {
                childCount = childCount - childCount % spanCount;
                if (pos >= childCount)// 如果是最后一列，则不需要绘制右边
                    return true;
            }
        }else{
            int orientation = ((LinearLayoutManager) layoutManager)
                    .getOrientation();
            if(orientation == LinearLayoutManager.VERTICAL){
                return true;
            }
        }
        return false;
    }

    /**
     * 判断是否是最后一行
     * @param parent
     * @param pos
     * @param spanCount
     * @param childCount
     * @return
     */
    private boolean isLastRaw(RecyclerView parent, int pos, int spanCount,
                              int childCount) {
        childCount --;//childcount从1开始算，所以应该先减1
        LayoutManager layoutManager = parent.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager)
        {
            childCount = childCount - childCount % spanCount;
            if (pos >= childCount){// 如果是最后一行，则不需要绘制底部
                return true;}
        } else if (layoutManager instanceof StaggeredGridLayoutManager)
        {
            int orientation = ((StaggeredGridLayoutManager) layoutManager)
                    .getOrientation();
            // StaggeredGridLayoutManager 且纵向滚动
            if (orientation == StaggeredGridLayoutManager.VERTICAL)
            {
                childCount = childCount - childCount % spanCount;
                // 如果是最后一行，则不需要绘制底部
                if (pos >= childCount)
                    return true;
            } else
            // StaggeredGridLayoutManager 且横向滚动
            {
                // 如果是最后一行，则不需要绘制底部
                if ((pos + 1) % spanCount == 0)
                {
                    return true;
                }
            }
        }else if(layoutManager instanceof LinearLayoutManager){
            childCount = childCount - childCount % spanCount;
            if (pos >= childCount){// 如果是最后一行，则不需要绘制底部
                return true;}
        }
        return false;
    }

    @Override
    public void getItemOffsets(Rect outRect, int itemPosition,
                               RecyclerView parent)
    {
        int spanCount = getSpanCount(parent);
        int childCount = parent.getAdapter().getItemCount();

        if((parent.getAdapter() instanceof BaseRecyclerAdapter) && ((BaseRecyclerAdapter)parent.getAdapter()).getHeaderView()!=null){
            itemPosition --;
            childCount --;
            outRect.set(0, 0, 0,mDividerHeight);
        }
        if(itemPosition >=0){
            setRect(outRect,itemPosition,parent,spanCount,childCount);
        }

    }

    private void setRect(Rect outRect, int itemPosition,RecyclerView parent,int spanCount,int childCount){
        if (isLastRaw(parent, itemPosition, spanCount, childCount))// 如果是最后一行，则不需要绘制底部
        {
            outRect.set(0, 0, mDividerHeight, 0);
        }
        else if (isLastColum(parent, itemPosition, spanCount, childCount))// 如果是最后一列，则不需要绘制右边
        {
            outRect.set(0, 0, 0, mDividerHeight);
        }
        else
        {
            outRect.set(0, 0, mDividerHeight,mDividerHeight);
        }
//        outRect.set(0, 0, 0,mDividerHeight);
    }

    public void setConfig(BaseRecyclerAdapter adapter){
        this.adapter = adapter;
    }

    /**
     *  单位转换 比如sp转px
     * @param unit
     * @param size
     * @return
     */
    private float applyDimension(int unit, float size){
        return TypedValue.applyDimension(unit, size, Resources.getSystem().getDisplayMetrics());
    }
}
